
package ar.com.lemil.j2me.serialization;

import java.io.*;
import java.util.Hashtable;



public class ObjectInputStream extends InputStream
    implements IObjectInputStream
{

    public ObjectInputStream(byte abyte0[])
        throws IOException
    {
        offset = 0;
        classCache = new Hashtable(3);
        if(abyte0 == null || abyte0.length == 0)
        {
            throw new IOException("Data is null or empty");
        } else
        {
            data = abyte0;
            readHeader();
            return;
        }
    }

    public ObjectInputStream(InputStream inputstream)
        throws IOException
    {
        offset = 0;
        classCache = new Hashtable(3);
        ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
        do
        {
            int i = inputstream.read();
            if(i == -1)
                break;
            bytearrayoutputstream.write(i);
        } while(true);
        bytearrayoutputstream.close();
        inputstream.close();
        data = bytearrayoutputstream.toByteArray();
        if(data.length == 0)
        {
            throw new IOException("Data is empty");
        } else
        {
            readHeader();
            return;
        }
    }

    private void readHeader()
        throws IOException
    {
        byte abyte0[] = IConstants.HEADER;
        if(abyte0[0] != readByte() || abyte0[1] != readByte() || abyte0[2] != readByte())
            throw new InvalidFormatException();
        byte byte0 = readByte();
        if(abyte0[3] != byte0)
            throw new InvalidVersionException(byte0 + "");
        byte byte1 = readByte();
        for(int i = 0; i < byte1; i++)
        {
            byte byte2 = readByte();
            String s = readShortASC();
            classCache.put(new Byte(byte2), s);
        }

    }

    public Object readObject()
        throws IOException, ClassNotFoundException
    {
        byte byte0 = readByte();
        if(byte0 != 10)
            throw new IOException("Expected Object");
        byte byte1 = readByte();
        Object obj = classCache.get(new Byte(byte1));
        if(obj == null)
            throw new IOException("Class code not found: " + byte1);
        String s = (String)obj;
        Class class1 = Class.forName(s);
        try
        {
            obj = class1.newInstance();
        }
        catch(Exception exception)
        {
            throw new IOException("Error create instance. Maybe not found constructor without parameters : " + exception.getMessage());
        }
        if(!(obj instanceof ISerializable))
        {
            throw new IOException("Object is not implement ISerializable interface");
        } else
        {
            ISerializable iserializable = (ISerializable)obj;
            iserializable.readObject(this);
            return obj;
        }
    }

    public byte readByte()
        throws IOException
    {
        return data[offset++];
    }

    public char readChar()
        throws IOException
    {
        char c = (char)((data[offset++] & 0xff) << 8);
        c |= (char)(data[offset++] & 0xff);
        return c;
    }

    public short readShort()
        throws IOException
    {
        short word0 = (short)((data[offset++] & 0xff) << 8);
        word0 |= (short)(data[offset++] & 0xff);
        return word0;
    }

    public int readInt()
        throws IOException
    {
        int i = (data[offset++] & 0xff) << 24;
        i |= (data[offset++] & 0xff) << 16;
        i |= (data[offset++] & 0xff) << 8;
        i |= data[offset++] & 0xff;
        return i;
    }

    public long readLong()
        throws IOException
    {
        long l = ((long)data[offset++] & 255L) << 56;
        l |= ((long)data[offset++] & 255L) << 48;
        l |= ((long)data[offset++] & 255L) << 40;
        l |= ((long)data[offset++] & 255L) << 32;
        l |= ((long)data[offset++] & 255L) << 24;
        l |= ((long)data[offset++] & 255L) << 16;
        l |= ((long)data[offset++] & 255L) << 8;
        l |= (long)data[offset++] & 255L;
        return l;
    }

    public String readASC()
        throws IOException
    {
        short word0 = readShort();
        if(word0 == 0)
            return null;
        byte abyte0[] = new byte[word0];
        for(int i = 0; i < word0; i++)
            abyte0[i] = data[offset++];

        return new String(abyte0);
    }

    public String readShortASC()
        throws IOException
    {
        byte byte0 = readByte();
        if(byte0 == 0)
            return null;
        byte abyte0[] = new byte[byte0];
        for(int i = 0; i < byte0; i++)
            abyte0[i] = data[offset++];

        return new String(abyte0);
    }

    public String readUTF()
        throws IOException
    {
        short word0 = readShort();
        if(word0 == 0)
            return null;
        byte abyte0[] = new byte[word0];
        for(int i = 0; i < word0; i++)
            abyte0[i] = data[offset++];

        return new String(abyte0, "utf-8");
    }

    public String readShortUTF()
        throws IOException
    {
        byte byte0 = readByte();
        if(byte0 == 0)
            return null;
        byte abyte0[] = new byte[byte0];
        for(int i = 0; i < byte0; i++)
            abyte0[i] = data[offset++];

        return new String(abyte0, "utf-8");
    }

    public int read()
        throws IOException
    {
        return readByte();
    }

    public byte[] readBytes()
        throws IOException
    {
        int i = readInt();
        byte abyte0[] = new byte[i];
        for(int j = 0; j < i; j++)
            abyte0[j] = readByte();

        return abyte0;
    }

    public boolean readBoolean()
        throws IOException
    {
        return readByte() == 1;
    }

    public int[] readInts()
        throws IOException
    {
        int i = readInt();
        int ai[] = new int[i];
        for(int j = 0; j < i; j++)
            ai[j] = readInt();

        return ai;
    }

    private final byte data[];
    private int offset;
    private final Hashtable classCache;
}
